﻿#define _USE_MATH_DEFINES

#include <SFML/Graphics.hpp>

#include <iostream>
#include <cmath>

#include "Animation.h"
#include "Character.h"
#include "CthulhuBoss.h"

using namespace std;

//地图大小
#define MAP_WIDTH 1280*5
#define MAP_HEIGHT 720*5

//相机大小
#define SCREEN_WIDTH 1280
#define SCREEN_HEIGHT 720
#define GROUND MAP_HEIGHT - 100



int main()
{

	sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "SFML works!");
	window.setFramerateLimit(60);
	
	try {

		//背景加载
		sf::Texture backgroundTexture1;
		sf::Texture backgroundTexture2;
		sf::Texture backgroundTexture3;
		sf::Texture backgroundTexture4;
		sf::Sprite backgroundSprite1;
		sf::Sprite backgroundSprite2;
		sf::Sprite backgroundSprite3;
		sf::Sprite backgroundSprite4;
		Animation backgroundAnimation;
		backgroundSprite1.setScale(2.f, 2.f);
		backgroundSprite2.setScale(2.f, 2.f);
		backgroundSprite3.setScale(2.f, 2.f);
		backgroundSprite4.setScale(2.f, 2.f);
		backgroundSprite1.setPosition(0.f,3000);
        backgroundSprite2.setPosition(0.f, 3000);
		backgroundSprite3.setPosition(0.f, 3000);
		backgroundSprite4.setPosition(0.f, 3000);
		backgroundAnimation.addBackground("resource/action/backgrounds/Background_1.png", backgroundTexture1, backgroundSprite1);
		backgroundAnimation.addBackground("resource/action/backgrounds/Background_2.png", backgroundTexture2, backgroundSprite2);
		backgroundAnimation.addBackground("resource/action/backgrounds/Background_3.png", backgroundTexture3, backgroundSprite3);
		backgroundAnimation.addBackground("resource/action/backgrounds/Background_4.png", backgroundTexture4, backgroundSprite4);
		backgroundAnimation.setAnimation(0.5f, true);
		backgroundAnimation.play();

		Character player;

		CthulhuBoss cthulhuBoss;//克苏鲁boss

		std::cout << "纹理加载中。。。" << std::endl;


		sf::Texture cthulhuBossTexture1;
		sf::Texture cthulhuBossTexture2;
		sf::Texture cthulhuBossTexture3;
		sf::Sprite cthulhuBossSprite1;
		sf::Sprite cthulhuBossSprite2;
		sf::Sprite cthulhuBossSprite3;
		cthulhuBoss.animation.addFrame("resource/action/Eye_of_Cthulhu_(Phase_1)_1.png", cthulhuBossTexture1, cthulhuBossSprite1);
		cthulhuBoss.animation.addFrame("resource/action/Eye_of_Cthulhu_(Phase_1)_2.png", cthulhuBossTexture2, cthulhuBossSprite2);
		cthulhuBoss.animation.addFrame("resource/action/Eye_of_Cthulhu_(Phase_1)_3.png", cthulhuBossTexture3, cthulhuBossSprite3);
		cthulhuBoss.animation.setAnimation(0.10f, true);
        cthulhuBoss.animation.setFaceDirection(true);
        cthulhuBoss.animation.play();

		//创建动画
		sf::Texture idleTexture1;
		sf::Texture idleTexture2;
		sf::Texture idleTexture3;
		sf::Texture idleTexture4;
		sf::Sprite idleSprite1;
		sf::Sprite idleSprite2;
		sf::Sprite idleSprite3;
		sf::Sprite idleSprite4;
		player.idleAnimation.addFrame("resource/action/Idle1.png", idleTexture1, idleSprite1);
		player.idleAnimation.addFrame("resource/action/Idle2.png", idleTexture2, idleSprite2);
		player.idleAnimation.addFrame("resource/action/Idle3.png", idleTexture3, idleSprite3);
		player.idleAnimation.addFrame("resource/action/Idle4.png", idleTexture4, idleSprite4);
		// 设置动画属性（每帧0.15秒，循环播放）
		player.idleAnimation.setAnimation(0.10f, true);
		//player.idleAnimation.setScale(2);
		player.idleAnimation.play();

		sf::Texture walkTexture1;
		sf::Texture walkTexture2;
		sf::Texture walkTexture3;
		sf::Texture walkTexture4;
		sf::Sprite walkSprite1;
		sf::Sprite walkSprite2;
		sf::Sprite walkSprite3;
		sf::Sprite walkSprite4;
		player.walkAnimation.addFrame("resource/action/Walk1.png", walkTexture1, walkSprite1);
		player.walkAnimation.addFrame("resource/action/Walk2.png", walkTexture2, walkSprite2);
		player.walkAnimation.addFrame("resource/action/Walk3.png", walkTexture3, walkSprite3);
		player.walkAnimation.addFrame("resource/action/Walk4.png", walkTexture4, walkSprite4);
		player.walkAnimation.setAnimation(0.10f, false);
		//player.walkAnimation.setScale(2);
		player.walkAnimation.play();

		sf::Texture jumpTexture;
		if (!jumpTexture.loadFromFile("resource/action/Walk2.png")) {
			std::cerr << "无法加载跳跃图片" << std::endl;
		}
		player.jumpSprite.setTexture(jumpTexture);
		player.jumpSprite.setOrigin(player.jumpSprite.getLocalBounds().width / 2, player.jumpSprite.getLocalBounds().height / 2);

		//武器
		if (!player.weaponTexture.loadFromFile("resource/action/weapon_m4_diamond.png")) {
			std::cerr << "无法加载武器图片" << std::endl;
		}

		player.weaponSprite.setTexture(player.weaponTexture);
		player.weaponSprite.setScale(0.7, 0.7);
		player.weaponSprite.setOrigin(player.weaponSprite.getLocalBounds().width / 2, player.weaponSprite.getLocalBounds().height / 2);

		if (!player.bulletTexture.loadFromFile("resource/action/bullet1.png")) {
			std::cerr << "无法加载子弹图片" << std::endl;
		}


		sf::Texture boomTexture1;
		sf::Texture boomTexture2;
		sf::Texture boomTexture3;
		sf::Texture boomTexture4;
		player.boomTextures.push_back(boomTexture1);
		player.boomTextures.push_back(boomTexture2);
		player.boomTextures.push_back(boomTexture3);
		player.boomTextures.push_back(boomTexture4);

		sf::Texture testTexture;
		if (!testTexture.loadFromFile("resource/action/Eye_of_Cthulhu_(Phase_1)_1.png")) {
			std::cerr << "无法加载测试图形" << std::endl;
		}
		player.testSprite.setTexture(testTexture);

		std::cout << "加载完成" << std::endl;

		//测试物体矢量移动
		sf::Sprite testObject;
		testObject.setTexture(player.weaponTexture);
		testObject.setPosition(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);
		testObject.setOrigin(testObject.getLocalBounds().width / 2, player.weaponSprite.getLocalBounds().height / 2);
		testObject.setScale(-1, 1);

		// 创建地面
		sf::RectangleShape ground(sf::Vector2f(MAP_WIDTH, 100));
		ground.setPosition(0, MAP_HEIGHT - 100);
		ground.setFillColor(sf::Color(90, 60, 30));

		// 创建背景
		sf::RectangleShape background(sf::Vector2f(MAP_WIDTH, MAP_HEIGHT));
		background.setPosition(0, 0);
		background.setFillColor(sf::Color(sf::Color::Black));

		// 创建视图
		sf::View view;
		view.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);

		// 创建UI文本
		sf::Font font;
		if (!font.loadFromFile("resource/font/arial.ttf")) {
			// 如果找不到字体，创建一个简单的替代
			std::cerr << "Failed to load font. Using default." << std::endl;
		}

		sf::Text infoText;
		infoText.setFont(font);
		infoText.setCharacterSize(16);
		infoText.setFillColor(sf::Color::White);
		infoText.setPosition(10, 10);

		// 游戏时钟
		sf::Clock clock;

		while (window.isOpen())
		{
			// 计算帧时间增量
			float deltaTime = clock.restart().asSeconds();


			sf::Event event;
			while (window.pollEvent(event))
			{
				if (event.type == sf::Event::Closed)
					window.close();
			}

			if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
				window.close();

			//更新背景
			backgroundAnimation.update(deltaTime);

			// 更新视图中心为玩家位置
            sf::Vector2f newCenter = player.curPosition;
			sf::Vector2f viewSize = view.getSize();
			view.setCenter(player.curPosition);
			if (newCenter.x < viewSize.x / 2)
				newCenter.x = viewSize.x / 2;
			if (newCenter.y < viewSize.y / 2)
				newCenter.y = viewSize.y / 2;
			if (newCenter.x > MAP_WIDTH - viewSize.x / 2)
				newCenter.x = MAP_WIDTH - viewSize.x / 2;
			if (newCenter.y > MAP_HEIGHT - viewSize.y / 2)
				newCenter.y = MAP_HEIGHT - viewSize.y / 2;

			view.setCenter(newCenter);
			window.setView(view);

			
			// 获取鼠标位置并正确转换为世界坐标
			sf::Vector2i mousePixelPos = sf::Mouse::getPosition(window);
			sf::Vector2f mouseWorldPos = window.mapPixelToCoords(mousePixelPos, view);
			// 2. 获取物体位置
			sf::Vector2f objectPos = player.weaponSprite.getPosition();

			// 3. 计算方向向量
			float dx = mouseWorldPos.x - objectPos.x;
			float dy = mouseWorldPos.y - objectPos.y;

			// 4. 计算夹角（弧度 -> 角度）
			float angleRad = std::atan2(dy, dx);
			float angleDeg = angleRad * 180 / static_cast<float>(M_PI);


			// 将负角度转换为0-360范围
			if (angleDeg < 0) angleDeg += 360;

			player.angleRad = angleRad;

			//std::cout << "角度：" << angleDeg << std::endl;


			// 矢量移动：位置 += 速度 * 时间
			sf::Vector2f velocity = sf::Vector2f(cos(angleRad), sin(angleRad)) * 10.f;
			//std::cout << "速度：" << velocity.x << ", " << velocity.y << std::endl;
			//testObject.move(velocity * deltaTime);

			// 5. 旋转物体（指向鼠标）
			//testObject.setRotation(angleDeg);

			player.handleInput(deltaTime);

			player.cthulhuBossSprite = cthulhuBoss.animation.getCurrentFrameSprite();
			// 更新动画
			player.update(deltaTime, cthulhuBoss.health);

			//克苏鲁存活 传递坐标
			if (cthulhuBoss.isAlive)
			{					
				cthulhuBoss.targetPosition = sf::Vector2f(player.curPosition.x, player.curPosition.y);				
			}

			cthulhuBoss.update(deltaTime);

			// 更新UI文本
			std::string info = "player world position: (" + std::to_string(static_cast<int>(player.curPosition.x)) +
				", " + std::to_string(static_cast<int>(player.curPosition.y)) + ")\n";
			info += "mouse world postion: (" + std::to_string(static_cast<int>(mouseWorldPos.x)) +
				", " + std::to_string(static_cast<int>(mouseWorldPos.y)) + ")\n";
			info += "player rotation: " + std::to_string(static_cast<int>(angleDeg)) + "";
			

			infoText.setString(info);
			
			// 渲染
			window.clear();
			window.draw(background);
			backgroundAnimation.drawBackground(window);
			window.draw(ground);
			cthulhuBoss.draw(window);
			player.draw(window);  

			// 重置为默认视图以绘制UI
			window.setView(window.getDefaultView());
			
			// 绘制克苏鲁血条
			cthulhuBoss.drawHealthBar(window);

			// 绘制UI文本
			window.draw(infoText);
			window.display();


		}
	} 
	catch (const std::exception& e) {
		std::cerr << "SFML Exception: " << e.what() << std::endl;
	}

	return 0;
}